/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.group;

import com.workcell.workdata.im.annotation.DeleteAction;
import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.annotation.UpdateAction;
import com.workcell.workdata.im.entity.GroupMember;
import com.workcell.workdata.im.model.request.DeleteMemberRequest;
import com.workcell.workdata.im.model.request.GroupAlisaRequest;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.model.vo.GroupMemberVO;
import com.workcell.workdata.im.service.GroupMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/group/member")
@Api(produces = "application/json", tags = "群成员相关接口" )
@Validated
public class MemberController {

	@Resource
	private GroupMemberService groupMemberService;

	@ApiOperation(httpMethod = "GET", value = "获取群组成员列表")
	@ApiImplicitParam(name = "id", value = "群组ID", paramType = "path",  dataTypeClass = Long.class,example = "0")
	@GetMapping(value = "/list/{id}")
	public ResponseEntity<List<GroupMemberVO>> list(@PathVariable long id) {
		return ResponseEntity.ok(groupMemberService.findList(id));
	}

	@ApiOperation(httpMethod = "DELETE", value = "退出群组")
	@ApiImplicitParam(name = "id", value = "群组ID", paramType = "path",  dataTypeClass = Long.class,example = "0")
	@DeleteMapping(value = "/quit/{id}")
	public ResponseEntity<Void> quit(@PathVariable long id, @ApiIgnore @UID Long uid) {

		GroupMember member = new GroupMember();
		member.setGroupId(id);
		member.setUid(uid);
		groupMemberService.remove(member);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "移除群成员")
	@PostMapping(value = "/delete")
	@Validated(DeleteAction.class)
	public ResponseEntity<Void> delete(@Valid @RequestBody DeleteMemberRequest request) {

		groupMemberService.remove(request.getIdList(), request.getGroupId());

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "设置群昵称")
	@PostMapping(value = "/alias")
	@Validated(UpdateAction.class)
	public ResponseEntity<Void> alias(@ApiIgnore @UID Long uid,@Valid @RequestBody GroupAlisaRequest request) {
		groupMemberService.setAlias(uid,request);
		return ResponseEntity.ok();
	}
}
